<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp"%>
<%!
	public Map getMap(List<Map> result , Map<String,String> ssMap){
		int sumCount = 0;
		//处理结合数据
		for(Map map : result){
		
			String count = map.get("cont") == null ? "0" : map.get("cont").toString();
		
			String ssdj = map.get("ssdj") == null ? "0" : map.get("ssdj").toString();
			map.put("name",ssMap.get(ssdj));
			
			sumCount = sumCount + Integer.parseInt(count);
		}
		
		Map<String,Object> resMap = new HashMap<String,Object>();
		resMap.put("rank",result);
		resMap.put("sumCount",sumCount+"");
		return resMap;
	}
%>
<%
	//统计手术人数
	String yydm =  input.getDesc("yydm","01");//医院代码
	String date = input.getDesc("start_date","2018-04-03");//开始时间
	String endDa = input.getDesc("end_date","2018-04-03");//结束时间
	String ksdm =  input.getDesc("ksdm","-1");//科室代码
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
	String nowDate = sdf2.format(sdf.parse(date));
	String nowEndDa = sdf2.format(sdf.parse(endDa));
	
	String startDate = nowDate+"00:00:00";
	String endDate = nowEndDa+"23:59:59";
	
	String sqlWhere = "";
	if(!ksdm.equals("-1")){
		sqlWhere += " and a.ksdm in (" + ksdm + ") ";
		
	}
	if(!yydm.equals("-1"))
		sqlWhere += " and a.ksdm in (select new map (id as id) from his_dept where yydm ='"+yydm+"') " ;
		
	//获取所有科室
	List<Map> deptList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from his_dept ");
	Map<String,String> deptMap = new HashMap<String,String>();
	//科室放到map中 key-value(id-name)
	for(Map map : deptList){
		deptMap.put(map.get("id").toString(),map.get("name").toString());
	}
	//获取所有等级
	List<Map> ssList = QBuilder.db("basedb").queryHql("select new map(id as id , name as name ) from SS_SSDJDMK ");
	Map<String,String> ssMap = new HashMap<String,String>();
	//等级放到map中 key-value(id-name)
	for(Map map : ssList){
		ssMap.put(map.get("id").toString(),map.get("name").toString());
	}
	
	
	//手术等级分类汇总
	String hql = "select new map(count(a) as cont ,a.ssdj as ssdj) from SS_SSDJK a where kssj >= '" + startDate + "' and kssj <= '" + endDate + "' and ssdj is not  null "+ sqlWhere +" group by ssdj   order by count(a) desc";
	List<Map> result = QBuilder.db("basedb").queryHql(hql);
	
	Map<String,Object> resMap = new HashMap<String,Object>();
	resMap.put("ssMap",getMap(result , ssMap));
	
	//新加功能
	List<Map> mapDetail1 = new ArrayList<Map>();
	List<Map> mapDetail2 = new ArrayList<Map>();
	List<Map> mapDetail3 = new ArrayList<Map>();
	List<Map> mapDetail4 = new ArrayList<Map>();
	String hqlDetail = "select new map(a.patid as patid , a.hzxm as hzxm ,a.ssmc as ssmc ,a.sqrq as sqrq ,a.ksdm as ksdm ,a.ssdj as ssdj) from SS_SSDJK a where kssj >= '" + startDate + "' and kssj <= '" + endDate + "' and ssdj is not  null " + sqlWhere + " and jlzt in(1,2) order by sqrq desc";
	List<Map> resultDetail = QBuilder.db("basedb").queryHql(hqlDetail);
	for(Map map : resultDetail){
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddhh:mm:ss");
		SimpleDateFormat sdf4 = new SimpleDateFormat("MM-dd");
		map.put("sqrq",sdf4.format(sdf3.parse(map.get("sqrq").toString())));
		String ksid = map.get("ksdm") == null ? "" : map.get("ksdm").toString();
		map.put("ksmc",deptMap.get(ksid));
		String ssdj = map.get("ssdj") == null ? "" : map.get("ssdj").toString();
		if("1".equals(ssdj)){
			mapDetail1.add(map);
		}else if("2".equals(ssdj)){
			mapDetail2.add(map);
		}else if("3".equals(ssdj)){
			mapDetail3.add(map);
		}else if("4".equals(ssdj)){
			mapDetail4.add(map);
		}
		String ssmc = map.get("ssmc") == null ? "" : map.get("ssmc").toString();
		if(ssmc.length() > 0){
			String re = ssmc.substring(ssmc.length()-1);
			if(re.equals("]")){
				ssmc = ssmc + "  ";
				map.put("ssmc",ssmc);
			}
		}
		
	}
	
	resMap.put("dj1",mapDetail1);
	resMap.put("dj2",mapDetail2);
	resMap.put("dj3",mapDetail3);
	resMap.put("dj4",mapDetail4);
	Message msg = new Message();

	msg.put("operationMap",resMap);
	out.println(msg);
%>